package middleware

import (
	"github.com/go-chi/chi/v5/middleware"
	"net/http"
	"gsc-datasets/pkg/util"
)

func WithWriteRes() func(http.Handler) http.Handler {
	return func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			ctx := r.Context()
			if w.Header().Get(util.RequestIDKey) == "" {
				w.Header().Add(
					util.RequestIDKey,
					middleware.GetReqID(ctx),
				)
			}
			next.ServeHTTP(w, r.WithContext(ctx))
		})
	}
}
